<?php

namespace app\common\behavior;

use app\common\lib\file_operate\FileOperate;
use app\common\lib\file_operate\MakeZip;
use app\common\lib\user_operate\ModuleOperate;
use app\common\model\PluginDbTableModel;

/**
 * 跨域请求  行为  绑定在app/tags.php  中
 */
class PluginTestingHook
{
    public function run(&$params)
    {
        $status = $params['info']['status'];

        //如果是开发中
        if ($status != 4) {

            $params['return'] = true;

            return;
        }

        $info = $params['info'];

        $moduleName = cmf_parse_name($info['name']);

        $mo = new ModuleOperate();
        //获取应用下载地址
        $mo->storeId = $info['store_id'];

        $res = $mo->getSourceAddr();

        if ($res['status'] != 'success') {

            self::returnStr('获取应用下载地址', $res['message'], 201);
        } else {

            self::returnStr('获取应用下载地址', '下载地址获取成功', 200);
        }
        //远程下载
        $fo = new FileOperate();

        $res = $fo->fileDownload($res['data'], $path = PLUGINS_PATH . '6785874456776558.zip');

        if ($res['status'] != 'success') {

            self::returnStr('下载应用', $res['message'], 201);

            $res['data'] = null;
        } else {

            self::returnStr('下载应用', '应用下载成功', 200);
        }
        //文件解压
        $MZ = new MakeZip();

        $res = $MZ->unZip($res['data'], $path = PLUGINS_PATH);

        if ($res['status'] != 'success') {

            self::returnStr('应用解压', $res['message'], 201);
        } else {

            self::returnStr('应用解压', '应用解压成功', 200);
        }
        //检测应用类是否已经定义
        $res = cmf_get_plugin_class(cmf_parse_name($moduleName, 1));

        if (class_exists($res)) {

            self::returnStr('检测应用类是否已经定义', '已经定义', 200);
        } else {

            self::returnStr('检测应用类是否已经定义', '未定义', 201);
        }
        //检查所需应用是否存在
        $PDM = new PluginDbTableModel();

        $res = $PDM->checkDemandModuleIsExists($moduleName);

        if ($res != true) {

            self::returnStr('检查所需应用是否存在', $res['message'], 201);
        } else {

            self::returnStr('检查所需应用是否存在', '检测通过', 200);
        }
        //检查是否配置pluginInfo 检查所有应用名字
        $res = $PDM->checkModuleInfo($moduleName);

        if ($res == true) {

            self::returnStr('检查应用pluginInfo配置', '已配置pluginInfo', 200);
        } else {

            self::returnStr('检查应用pluginInfo配置', $res['message'], 201);
        }
        //检测需求表，需求应用是否存在
        $res = $PDM->chechModuleAndTableIsExists($moduleName);

        if ($res == false || $res == 0) {

            self::returnStr('检测需求表', '检测失败', 201);
        } else {

            self::returnStr('检测需求表', '检测通过', 200);
        }

        $params['return'] = self::$returnStrArray;
    }

    /**
     * 状态返回
     * @return [type] [description]
     */
    protected static $returnStrArray = [];

    private static function returnStr($item = '', $msg = '', $code = 200, $operate = 'empty')
    {
        $data = [
            'item'    => $item,
            'msg'     => $msg,
            'code'    => $code,
            'operate' => $operate,
        ];

        self::$returnStrArray[] = $data;
    }

}
